package com.janal.controller.Modify;

import com.janal.model.user.User;
import com.janal.service.modify.ModifyService;
import com.janal.service.modify.ModifyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ModifyMessageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1、从前端获取修改后的信息,并进行判断
        HttpSession session = req.getSession();
        String usercode = (String) session.getAttribute("usercode");
        String nickname = req.getParameter("nickname");
        String user_face = req.getParameter("user_face");
        String user_sex = req.getParameter("user_sex");
        String user_email = req.getParameter("user_email");
        if("".equals(nickname)||"".equals(user_face)||"".equals(user_sex)||"".equals(user_email)){
            req.setAttribute("modifyError","个人信息不能为空！");
            req.getRequestDispatcher("Modify.jsp").forward(req,resp);
            return;
        }
//        2、将信息存储到对象user中
        User user = new User(usercode,null,nickname,user_face,user_sex,user_email);
//        3、调用service修改用户信息
        ModifyService ms = new ModifyServiceImpl();
        if(ms.modifyMessage(user)){
            req.setAttribute("modifySuccess","修改成功！");
            resp.sendRedirect("/p/Modify.jsp");
        }else {
            req.setAttribute("modifyError","信息过长，修改失败！");
            req.getRequestDispatcher("Modify.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
